package com.dl698.axdr;

import com.dl698.asn1.axdr.AxdrType;
import com.dl698.util.ReverseByteArrayInputStream;
import com.dl698.util.ReverseByteArrayOutputStream;

/**
 * OAD类
 * 实现对象标识和属性的编解码
 * 
 * @author wind
 * @version 1.1.1
 */
public class OAD implements AxdrType {

    private byte[] dataCode = null;
    private Unsigned16 oi = null;
    private OBI obi = null;
    private Unsigned8 index = null;

    /**
     * 默认构造函数
     */
    public OAD() {
    }

    /**
     * 设置数据编码
     * 
     * @param dataCode 数据编码
     */
    public void setDataCode(byte[] dataCode) {
        this.dataCode = dataCode;
    }

    /**
     * 设置OAD参数
     * 
     * @param oi    对象标识
     * @param obi   对象属性标识
     * @param index 索引
     */
    public void setOad(Unsigned16 oi, OBI obi, Unsigned8 index) {
        this.oi = oi;
        this.obi = obi;
        this.index = index;
    }

    /**
     * 获取对象标识
     * 
     * @return 对象标识
     */
    public Unsigned16 getOI() {
        if (oi != null) {
            return oi;
        } else {
            throw new RuntimeException("oi is null");
        }
    }

    /**
     * 获取对象属性标识
     * 
     * @return 对象属性标识
     */
    public OBI getOBI() {
        if (obi != null) {
            return obi;
        } else {
            throw new RuntimeException("obi is null");
        }
    }

    /**
     * 获取索引
     * 
     * @return 索引
     */
    public Unsigned8 getIndex() {
        if (index != null) {
            return index;
        } else {
            throw new RuntimeException("index is null");
        }
    }

    /**
     * 编码并保存
     * 
     * @param encodingSizeGuess 编码大小预估
     */
    public void encodeAndSave(int encodingSizeGuess) {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        encode(revOStream);
        this.dataCode = revOStream.getArray();
    }

    @Override
    public int decode(ReverseByteArrayInputStream input) {
        int codeLength = 0;
        oi = new Unsigned16();
        oi.setConst();
        codeLength += oi.decode(input);

        obi = new OBI();
        obi.setNumBit(8);
        codeLength += obi.decode(input);

        index = new Unsigned8();
        index.setConst();
        codeLength += index.decode(input);

        return codeLength;
    }

    @Override
    public int encode(ReverseByteArrayOutputStream output) {
        int codeLength;
        if (dataCode != null) {
            codeLength = dataCode.length;
            for (int i = dataCode.length - 1; i >= 0; i--) {
                try {
                    output.write(dataCode[i]);
                } catch (java.io.IOException e) {
                    throw new RuntimeException(e);
                }
            }
        } else {
            if (oi != null && obi != null && index != null) {
                codeLength = 0;
                codeLength += index.encode(output);
                codeLength += obi.encode(output);
                codeLength += oi.encode(output);
            } else {
                throw new RuntimeException("oi || obi || index is null");
            }
        }
        return codeLength;
    }

    @Override
    public String toString() {
        if (oi != null && obi != null && index != null) {
            return "sequence: {" + "oi: " + oi + ", obi: " + obi + ", index: " + index + "}";
        } else {
            return "oi || obi || index is null";
        }
    }

    /**
     * 获取数据编码
     * 
     * @return 数据编码
     */
    public byte[] getDataCode() {
        return dataCode;
    }
}